package com.company.zoj;

import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

/**
 * Created by v on 17-6-4.
 */
public class ZojTest1654 {
    private static class Row{
        int begin;
        int end;
        int row;

        public Row(int begin,int end,int row){
            this.begin=begin;
            this.end=end;
            this.row =row;
        }
    }

    private static class Column{
        int begin;
        int end;
        int column;

        public Column(int begin,int end,int column){
            this.begin=begin;
            this.end=end;
            this.column =column;
        }
    }

    private static char[][] map;

    private static List<Row> rows;
    private static List<Column> columns;

    private static int n;
    private static int m;

    private static boolean[][] maze;
    private static int match[];
    private static boolean[] isVisited;

    private static void init(){
        rows=new LinkedList<>();
        columns=new LinkedList<>();

        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(map[i][j]=='o'){
                    int begin=j;
                    while (begin>=0){
                        if(map[i][begin]!='#'){
                            begin--;
                        }else {
                            break;
                        }
                    }
                    begin++;

                    while (j<m){
                        if(map[i][j]!='#'){
                            j++;
                        }else {
                            break;
                        }
                    }

                    rows.add(new Row(begin,j-1,i));
                }
            }
        }

        for(int j=0;j<m;j++){
            for(int i=0;i<n;i++){
                if(map[i][j]=='o'){
                    int begin=i;
                    while (begin>=0){
                        if(map[begin][j]!='#'){
                            begin--;
                        }else {
                            break;
                        }
                    }
                    begin++;

                    while (i<n){
                        if(map[i][j]!='#'){
                            i++;
                        }else {
                            break;
                        }
                    }

                    columns.add(new Column(begin,i-1,j));
                }
            }
        }

        maze=new boolean[rows.size()][columns.size()];
        for(int i=0;i<rows.size();i++){
            for(int j=0;j<columns.size();j++){
                maze[i][j]=false;
            }
        }
        for(int i=0;i<rows.size();i++){
            for(int j=0;j<columns.size();j++){
                if(rows.get(i).row>=columns.get(j).begin&& rows.get(i).row<= columns.get(j).end
                        && columns.get(j).column>= rows.get(i).begin&& columns.get(j).column<= rows.get(i).end
                        &&map[rows.get(i).row][columns.get(j).column]=='o'){
                    maze[i][j]=true;
                }
            }
        }

        match=new int[columns.size()];
        for(int i=0;i<match.length;i++){
            match[i]=-1;
        }
    }

    public static boolean find(int u){
        for(int i=0;i<columns.size();i++){
            if(maze[u][i]&&!isVisited[i]){
                isVisited[i]=true;
                if(match[i]==-1||find(match[i])){
                    match[i]=u;
                    return true;
                }
            }
        }

        return false;
    }

    public static int solve(){
        int ans=0;
        for(int i=0;i<rows.size();i++){
            isVisited=new boolean[columns.size()];
            if(find(i)){
                ans++;
            }
        }

        return ans;
    }

    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        try {
            int cases=input.nextInt();

            for(int i=0;i<cases;i++){
                n=input.nextInt();
                m=input.nextInt();
                map=new char[n][m];
                for(int j=0;j<n;j++){
                    String line=input.next();
                    for(int k=0;k<m;k++){
                        map[j][k]=line.charAt(k);
                    }
                }

                init();
                System.out.println("Case :"+(i+1));
                System.out.println(solve());
            }
        }catch (Exception ex){
            return;
        }
    }
}
